package cn.sdormitory.controller;

import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.dto.ResultMsg;
import cn.sdormitory.smartclass.entity.AdClassDv;
import cn.sdormitory.smartclass.service.ClassDvService;
import cn.sdormitory.smartclass.vo.ClassDvVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @创建人：zhouyang
 * @创建时间：2021/12/22 15:33
 * @version：V1.0
 */
@RestController
@RequestMapping("/app/ClassDv")
@Api("APP端班级学员违规检查管理接口")
public class AppClassDvController {
    @Autowired
    private ClassDvService classDvService;
    @ApiOperation("getAppClaStuDvListByCId => 查询某班级下的所有学员某日某时间段的违规情况")
    @GetMapping(value = "/getAppClaStuAttenceListByCId")
    public ResultMsg listDorAttence(Long classId, String dvDate,String timeSlot){
        List<ClassDvVo> classAttenceVoList=classDvService.getAppClaStuDvByClaIdAndSlot(classId,dvDate,timeSlot);
        return ResultMsg.BY_SUCCESS("获取成功",classAttenceVoList);
    }
    @ApiOperation("create => APP端新增班级学员违规情况")
    @PostMapping(value = "/createClassDv")
    public CommonResult<Integer> create(@RequestBody AdClassDv adClassDv) throws IOException {
        int count = classDvService.addClassDv(adClassDv);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("create => APP端教官登记违纪")
    @PostMapping(value = "/createInstructorRecordDv")
    public CommonResult<Integer> createInstructorRecordDv(AdClassDv adClassDv,@RequestParam(value = "upload") MultipartFile upload) throws IOException {
            byte[] pictureData = null;
            if(!upload.isEmpty()){
                // 使用输入流获取上传的图片
                InputStream inputStream = null;
                try {
                    inputStream = upload.getInputStream();
                    // 定义字节数组用于存放图片信息
                    pictureData = new byte[(int) upload.getSize()];
                    // 读取图片到字节数组中
                    inputStream.read(pictureData);
                    adClassDv.setPhoto(pictureData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //创建时为未处罚状态
            adClassDv.setPunishStatus("0");
            int count = classDvService.createInstructorRecordDv(adClassDv);
            if (count > 0) {
                return CommonResult.success(count);
            }

        return CommonResult.failed();
    }

    @ApiOperation("create => APP端查询登录人提交的违规记录")
    @GetMapping(value = "/getDvHistoryByUserName")
    public ResultMsg getDvHistoryByUserName(String userName) throws IOException {
        List<ClassDvVo> list = classDvService.getDvHistoryByUserName(userName);
        return ResultMsg.BY_SUCCESS("获取成功",list);
    }

    @ApiOperation("create => APP端根据违规ID查询违规记录详情")
    @GetMapping(value = "/getDvDetailById")
    public ResultMsg getDvDetailById(String id) throws IOException {
        ClassDvVo vo = classDvService.getDvDetailById(id);
        return ResultMsg.BY_SUCCESS("获取成功",vo);
    }

    @ApiOperation("create => APP端修改违纪处罚状态")
    @PostMapping(value = "/updatePunishStatus")
    public ResultMsg updatePunishStatus(@RequestBody ClassDvVo vo) throws IOException {
        int num = classDvService.updatePunishStatus(vo);
        return ResultMsg.BY_SUCCESS("修改成功",num);
    }



}
